home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Cafe 3
/
Visual Cafe 3.ISO
/
Vcafe
/
JFC.bin
/
DefaultBoundedRangeModel.java
< prev
next >
Wrap
Text File
|
1998-06-30
|
10KB
|
348 lines
/*
* @(#)DefaultBoundedRangeModel.java 1.25 98/02/05
*
* Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*
*/
package com.sun.java.swing;
import com.sun.java.swing.event.*;
import java.io.Serializable;
/**
* A generic implementation of BoundedRangeModel.
* <p>
* Warning: serialized objects of this class will not be compatible with
* future swing releases. The current serialization support is appropriate
* for short term storage or RMI between Swing1.0 applications. It will
* not be possible to load serialized Swing1.0 objects with future releases
* of Swing. The JDK1.2 release of Swing will be the compatibility
* baseline for the serialized form of Swing objects.
*
* @version 1.25 02/05/98
* @author David Kloba
* @author Hans Muller
* @see BoundedRangeModel
*/
public class DefaultBoundedRangeModel implements BoundedRangeModel, Serializable
{
/**
* Only one ChangeEvent is needed per model instance since the
* event's only (read-only) state is the source property. The source
* of events generated here is always "this".
*/
protected transient ChangeEvent changeEvent = null;
protected EventListenerList listenerList = new EventListenerList();
private int value = 0;
private int extent = 0;
private int min = 0;
private int max = 100;
private boolean isAdjusting = false;
/**
* Initializes all of the properties as follows:
* <ul>
* <li><code>value</code> = 0
* <li><code>extent</code> = 0
* <li><code>minimum</code> = 0
* <li><code>maximum</code> = 100
* <li><code>adjusting</code> = false
* </ul>
*/
public DefaultBoundedRangeModel() {
}
/**
* Initializes value, extent, minimum and maximum. Adjusting is false.
* Throws an IllegalArgumentException if the following constraints
* aren't satisfied:
* <pre>
* min <= value <= value+extent <= max
* </pre>
*/
public DefaultBoundedRangeModel(int value, int extent, int min, int max)
{
if ((max >= min) && (value >= min) && ((value + extent) <= max)) {
this.value = value;
this.extent = extent;
this.min = min;
this.max = max;
}
else {
throw new IllegalArgumentException("invalid range properties");
}
}
/**
* @return the model's current value
* @see #setValue
* @see BoundedRangeModel#getValue
*/
public int getValue() {
return value;
}
/**
* @return the model's extent
* @see #setExtent
* @see BoundedRangeModel#getExtent
*/
public int getExtent() {
return extent;
}
/**
* @return the model's minimum
* @see #setMinimum
* @see BoundedRangeModel#getMinimum
*/
public int getMinimum() {
return min;
}
/**
* @return the model's maximum
* @see #setMaximum
* @see BoundedRangeModel#getMaximum
*/
public int getMaximum() {
return max;
}
/**
* Sets the value to <I>n</I> after ensuring that <I>n</I> falls
* within the model's constraints:
* <pre>
* minimum <= value <= value+extent <= maximum
* </pre>
*
* @see BoundedRangeModel#setValue
*/
public void setValue(int n) {
int newValue = Math.max(n, min);
if(newValue + extent > max) {
newValue = max - extent;
}
setRangeProperties(newValue, extent, min, max, isAdjusting);
}
/**
* Sets the extent to <I>n</I> after ensuring that <I>n</I>
* is greater than or equal to zero and falls within the model's
* constraints:
* <pre>
* minimum <= value <= value+extent <= maximum
* </pre>
*
* @see #getExtent
* @see BoundedRangeModel#setExtent
*/
public void setExtent(int n) {
int newExtent = Math.max(0, n);
if(value + newExtent > max) {
newExtent = max - value;
}
setRangeProperties(value, newExtent, min, max, isAdjusting);
}
/**
* Sets the minimum to <I>n</I> after ensuring that <I>n</I>
* that the other three properties obey the model's constraints:
* <pre>
* minimum <= value <= value+extent <= maximum
* </pre>
*
* @see #getMinimum
* @see BoundedRangeModel#setMinimum
*/
public void setMinimum(int n) {
int newMax = Math.max(n, max);
int newValue = Math.max(n, value);
int newExtent = Math.min(newMax - newValue, extent);
setRangeProperties(newValue, newExtent, n, newMax, isAdjusting);
}
/**
* Sets the maximum to <I>n</I> after ensuring that <I>n</I>
* that the other three properties obey the model's constraints:
* <pre>
* minimum <= value <= value+extent <= maximum
* </pre>
*
* @see #getMaximum
* @see BoundedRangeModel#setMaximum
*/
public void setMaximum(int n) {
int newMin = Math.min(n, min);
int newValue = Math.min(n, value);
int newExtent = Math.min(n - newValue, extent);
setRangeProperties(newValue, newExtent, newMin, n, isAdjusting);
}
/**
* Sets the valueIsAdjusting property.
*
* @see #getValueIsAdjusting
* @see #setValue
* @see BoundedRangeModel#setValueIsAdjusting
*/
public void setValueIsAdjusting(boolean b) {
setRangeProperties(value, extent, min, max, b);
}
/**
* Returns true if the value is in the process of changing
* as a result of actions being taken by the user.
*
* @return the value of the valueIsAdjusting property
* @see #setValueIsAdjusting
* @see #setValue
* @see BoundedRangeModel#getValueIsAdjusting
*/
public boolean getValueIsAdjusting() {
return isAdjusting;
}
/**
* Sets all of the BoundedRangeModel properties after forcing
* the arguments to obey the usual constraints:
* <pre>
* minimum <= value <= value+extent <= maximum
* </pre>
* <p>
* At most, one ChangeEvent is generated.
*
* @see BoundedRangeModel#setRangeProperties
* @see #setValue
* @see #setExtent
* @see #setMinimum
* @see #setMaximum
* @see #setValueIsAdjusting
*/
public void setRangeProperties(int newValue, int newExtent, int newMin, int newMax, boolean adjusting)
{
if(newMin > newMax)
newMin = newMax;
if(newValue > newMax)
newMax = newValue;
if(newValue < newMin)
newMin = newValue;
if((newExtent + newValue) > newMax)
newExtent = newMax - newValue;
if(newExtent < 0)
newExtent = 0;
boolean isChange =
(newValue != value) ||
(newExtent != extent) ||
(newMin != min) ||
(newMax != max) ||
(adjusting != isAdjusting);
if (isChange) {
value = newValue;
extent = newExtent;
min = newMin;
max = newMax;
isAdjusting = adjusting;
fireStateChanged();
}
}
/**
* Adds a ChangeListener. The change listeners are run each
* time any one of the Bounded Range model properties changes.
*
* @param l the ChangeListener to add
* @see #removeChangeListener
* @see BoundedRangeModel#addChangeListener
*/
public void addChangeListener(ChangeListener l) {
listenerList.add(ChangeListener.class, l);
}
/**
* Removes a ChangeListener.
*
* @param l the ChangeListener to remove
* @see #addChangeListener
* @see BoundedRangeModel#removeChangeListener
*/
public void removeChangeListener(ChangeListener l) {
listenerList.remove(ChangeListener.class, l);
}
/**
* Run each ChangeListeners stateChanged() method.
*
* @see #setRangeProperties
* @see EventListenerList
*/
protected void fireStateChanged()
{
Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -=2 ) {
if (listeners[i] == ChangeListener.class) {
if (changeEvent == null) {
changeEvent = new ChangeEvent(this);
}
((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
}
}
}
/**
* Returns a string that displays all of the BoundedRangeModel properties.
*/
public String toString() {
String modelString =
"value=" + getValue() + ", " +
"extent=" + getExtent() + ", " +
"min=" + getMinimum() + ", " +
"max=" + getMaximum() + ", " +
"adj=" + getValueIsAdjusting();
return getClass().getName() + "[" + modelString + "]";
}
}